package com.itheima.listener;

import com.itheima.domain.mongo.MovementScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 监听tanhua-app 通过MQ传递过来的消息
 */
@Component
@RocketMQMessageListener(consumerGroup = "tanhua-recommend-movement",topic = "tanhua-recommend-161")
public class MovementScoreListener implements RocketMQListener<Map> {


    @Autowired
    private MongoTemplate mongoTemplate;
    //监听器的方法
    //- 浏览 +  1
    //- 点赞 +  5
    //- 喜欢 +  8
    //- 评论 + 10
    @Override
    public void onMessage(Map map) {
        System.out.println("监听到了数据:" + map);
        Long userId = Long.valueOf(map.get("userId").toString()); //谁
        Long pid =  Long.valueOf(map.get("pid").toString());//什么内容
        Integer type = Integer.valueOf(map.get("type").toString());//什么操作



        //创建需要保存的对象
        MovementScore movementScore = new MovementScore();
        movementScore.setPublishId(pid);
        movementScore.setDate(System.currentTimeMillis());
        movementScore.setUserId(userId);

        switch (type) { //以后得分的计算规则, 按照真实开发需求文档进行计算
            case 1: {
                movementScore.setScore(20d);
                break;
            }
            case 2: {
                movementScore.setScore(1d);
                break;
            }
            case 3: {
                movementScore.setScore(5d);
                break;
            }
            case 4: {
                movementScore.setScore(8d);
                break;
            }
            case 5: {
                movementScore.setScore(10d);
                break;
            }
            case 6: {
                movementScore.setScore(-5d);
                break;
            }
            case 7: {
                movementScore.setScore(-8d);
                break;
            }
        }
        mongoTemplate.save(movementScore);//保存数据
    }


}
